package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.entity.Station;
import com.jiyun.entity.User;
import com.jiyun.service.IStationService;
import com.jiyun.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-05-05
 */
@RestController
@RequestMapping("/station")
public class StationController {

    @Autowired
    IStationService stationService;

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public void addStation(@RequestBody Station station){
        station.setState("启用");
        station.setCreateTime(new Date());
        stationService.save(station);
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateStation(@RequestBody Station station){
        stationService.updateById(station);
    }

    @Autowired
    IUserService userService;

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public String deleteOne(@RequestParam Integer id){


        List<User> list = userService.list();
        for (User user : list) {
            String[] split = user.getSids().split(",");
            for (String sid : split) {
                if(sid.equals(id+"")){
                    return "当前岗位有用户使用，不能删除";
                }
            }
        }


        //需要考虑关联数据删除问题
        stationService.removeById(id);
        return "OK";
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        stationService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public Station findById(@RequestParam Integer id){
        return stationService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<Station> findAll(){
        return stationService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<Station> findPage(@RequestParam Integer page,@RequestParam Integer pageSize,@RequestBody Station station){
        //分页
        Page<Station> stationPage = new Page<>(page, pageSize);
        //拼接条件集
        LambdaQueryWrapper<Station> stationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        stationLambdaQueryWrapper.like(StringUtils.isNotBlank(station.getCode()), Station::getCode,station.getCode());
        stationLambdaQueryWrapper.like(StringUtils.isNotBlank(station.getName()), Station::getName,station.getName());
        stationLambdaQueryWrapper.eq(StringUtils.isNotBlank(station.getState()), Station::getState,station.getState());

        //按照num正序排
        stationLambdaQueryWrapper.orderByAsc(Station::getNum);

        return stationService.page(stationPage,stationLambdaQueryWrapper);
    }

}
